#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 2345

int main(void)
{
    int sockfd;              //定义套接口描述符
    struct sockaddr_in addr; //定位IPv4套接口地址数据结构addr
    int addr_len = sizeof(struct sockaddr_in);
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) // set up a socket
    {
        perror("socket created error!\n");
        exit(1);
    }
    else
    {
        printf("socket created successfully!\n");
        printf("socket id:%d\n", sockfd);
    }
    bzero(&addr, sizeof(struct sockaddr_in)); //清空表示地址的结构体变量
    addr.sin_family = AF_INET;                //设置addr的成员变量
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = htonl(INADDR_ANY); // IP地址设为本机IP
    if (bind(sockfd, (struct sockaddr *)(&addr), sizeof(struct sockaddr)) < 0)
    {
        perror("bind error!\n");
        exit(1);
    }
    else
    {
        printf("bind port successfully!\n");
        printf("local port:%d\n", PORT);
    }
    return 0;
}